﻿@{
    Layout = null;
}
@Html.Hidden("UserIds")
@Html.Hidden("RoleId")
    <table id="UserTable" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0" width="100%"></table>
    <script>
        var UserIds = []
        var Users = {
            Search: function () {
                $("#UserTable").mainToolDataTable({
                    "ajax": {
                        url: '@Url.Action("PageUsers", "Users")',
                        type: 'post',
                        data: {
                            filter: {
                                Name: "",
                                Code: "",
                            }
                        }
                    },
                    "columns": [
                        { data: "Name", title: "角色名称" },
                        { data: "Code", title: "角色编码" },
                        {
                            title: "全选<input id='checkAll' type='checkbox' />",
                            orderable: false,
                            data: "Id",
                            style: "min-width:190px;",
                            render: function (data, type, row) {
                                if ($.inArray(data, $.parseJSON($("#UserIds").val())) >= 0 && $.inArray(data, UserIds) < 0)
                                    UserIds.push(data);
                                return "<input class='userIdClass' type='checkbox' value='" + data + "' " + ($.inArray(data, $.parseJSON($("#UserIds").val())) >= 0 ? "checked='checked'" : "") + " />";
                            }
                        },
                    ],
                    "preDrawCallback": function () {
                        UserIds = [];
                    },
                    "initComplete": function () {
                        $(".userIdClass").click(function () {
                            $("#checkAll").prop("checked", $("input.userIdClass").length == $("input.userIdClass:checked").length ? true : false);
                        });
                        $("#checkAll").prop("checked", $("input.userIdClass").length == $("input.userIdClass:checked").length ? true : false);
                    }
                });
            },
        };
        Roles.AllocateUsersSubmit = function () {
            var selectUserIds = new Array();
            $(".userIdClass").each(function (e, item) {
                if (item.checked) {
                    selectUserIds.push(Number($(item).val()));
                }
            });
            $.mainTool.ajax({
                url: '@Url.Action("AllocateUsersSubmit")',
                data: {
                    RoleId: Number($("#RoleId").val()),
                    UserIds: UserIds,
                    SelectUserIds: selectUserIds,
                },
                success: function (data) {
                    Users.Search();
                }
            });
        };
        $(document).ready(function () {
            Users.Search();
            $("#checkAll").click(function () {
                $('input.userIdClass').prop("checked", $(this).prop("checked"));
            });
        });
    </script>